package cn.harry.cabinet.theme.components

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import cn.harry.cabinet.R
import cn.harry.cabinet.theme.constants.ThemeConstants
import cn.harry.cabinet.utils.ColorUtils

/**
 * 语义化文本组件
 * 支持Success、Warning、Danger、Info、Neutral等语义化文本颜色和样式
 * 
 * 特性：
 * - 语义化颜色支持
 * - 主题自适应
 * - 多种文本层级 (Primary, Secondary, Tertiary)
 * - 文本大小预设 (Large, Medium, Small)
 * - 字体粗细预设
 * - 无障碍支持
 */
class SemanticTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {
    
    /**
     * 文本类型枚举
     */
    enum class TextType {
        PRIMARY,
        SUCCESS,
        WARNING,
        DANGER,
        INFO,
        NEUTRAL
    }
    
    /**
     * 文本层级枚举
     */
    enum class TextLevel {
        PRIMARY,    // 主要文本
        SECONDARY,  // 次要文本
        TERTIARY,   // 第三级文本
        DISABLED    // 禁用文本
    }
    
    /**
     * 文本大小枚举
     */
    enum class TextSize {
        LARGE,      // 大文本 18sp
        MEDIUM,     // 中等文本 16sp
        SMALL,      // 小文本 14sp
        CAPTION     // 说明文本 12sp
    }
    
    /**
     * 字体粗细枚举
     */
    enum class TextWeight {
        NORMAL,     // 正常
        MEDIUM,     // 中等
        BOLD        // 粗体
    }
    
    private var textType: TextType = TextType.NEUTRAL
    private var textLevel: TextLevel = TextLevel.PRIMARY
    private var textSizeType: TextSize = TextSize.MEDIUM
    private var textWeight: TextWeight = TextWeight.NORMAL
    private var isDarkMode: Boolean = false
    
    init {
        // 设置默认样式
        applySemanticStyle()
    }
    
    /**
     * 设置文本类型和层级
     */
    fun setTextStyle(
        type: TextType = TextType.NEUTRAL,
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL,
        darkMode: Boolean = false
    ) {
        this.textType = type
        this.textLevel = level
        this.textSizeType = size
        this.textWeight = weight
        this.isDarkMode = darkMode
        applySemanticStyle()
    }
    
    /**
     * 应用语义化样式
     */
    private fun applySemanticStyle() {
        // 设置文本颜色
        setTextColor(getSemanticTextColor())
        
        // 设置文本大小
        setTextSize(TypedValue.COMPLEX_UNIT_SP, getTextSizeInSp())
        
        // 设置字体粗细
        typeface = getSemanticTypeface()
    }
    
    /**
     * 获取语义化文本颜色
     */
    private fun getSemanticTextColor(): Int {
        return when (textType) {
            TextType.SUCCESS -> when (textLevel) {
                TextLevel.PRIMARY -> if (isDarkMode) R.color.semantic_success_text_dark else R.color.semantic_success_text
                TextLevel.SECONDARY -> if (isDarkMode) R.color.semantic_success_light else R.color.semantic_success_dark
                TextLevel.TERTIARY -> R.color.semantic_success_light
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
            
            TextType.WARNING -> when (textLevel) {
                TextLevel.PRIMARY -> if (isDarkMode) R.color.semantic_warning_text_dark else R.color.semantic_warning_text
                TextLevel.SECONDARY -> if (isDarkMode) R.color.semantic_warning_light else R.color.semantic_warning_dark
                TextLevel.TERTIARY -> R.color.semantic_warning_light
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
            
            TextType.DANGER -> when (textLevel) {
                TextLevel.PRIMARY -> if (isDarkMode) R.color.semantic_danger_text_dark else R.color.semantic_danger_text
                TextLevel.SECONDARY -> if (isDarkMode) R.color.semantic_danger_light else R.color.semantic_danger_dark
                TextLevel.TERTIARY -> R.color.semantic_danger_light
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
            
            TextType.INFO -> when (textLevel) {
                TextLevel.PRIMARY -> if (isDarkMode) R.color.semantic_info_text_dark else R.color.semantic_info_text
                TextLevel.SECONDARY -> if (isDarkMode) R.color.semantic_info_light else R.color.semantic_info_dark
                TextLevel.TERTIARY -> R.color.semantic_info_light
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
            
            TextType.NEUTRAL -> when (textLevel) {
                TextLevel.PRIMARY -> if (isDarkMode) R.color.neutral_text_primary_dark else R.color.neutral_text_primary
                TextLevel.SECONDARY -> if (isDarkMode) R.color.neutral_text_secondary_dark else R.color.neutral_text_secondary
                TextLevel.TERTIARY -> if (isDarkMode) R.color.neutral_text_tertiary_dark else R.color.neutral_text_tertiary
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
            
            TextType.PRIMARY -> when (textLevel) {
                TextLevel.PRIMARY -> R.color.theme_primary
                TextLevel.SECONDARY -> R.color.theme_primary_dark
                TextLevel.TERTIARY -> R.color.theme_primary_light
                TextLevel.DISABLED -> if (isDarkMode) R.color.neutral_text_disabled_dark else R.color.neutral_text_disabled
            }
        }.let { colorRes ->
            ContextCompat.getColor(context, colorRes)
        }
    }
    
    /**
     * 获取文本大小 (sp)
     */
    private fun getTextSizeInSp(): Float {
        return when (textSizeType) {
            TextSize.LARGE -> 18f
            TextSize.MEDIUM -> 16f
            TextSize.SMALL -> 14f
            TextSize.CAPTION -> 12f
        }
    }
    
    /**
     * 获取字体样式
     */
    private fun getSemanticTypeface(): Typeface {
        return when (textWeight) {
            TextWeight.NORMAL -> Typeface.DEFAULT
            TextWeight.MEDIUM -> Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
            TextWeight.BOLD -> Typeface.DEFAULT_BOLD
        }
    }
    
    /**
     * 设置成功文本样式
     */
    fun setAsSuccessText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.SUCCESS, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置警告文本样式
     */
    fun setAsWarningText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.WARNING, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置危险文本样式
     */
    fun setAsDangerText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.DANGER, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置信息文本样式
     */
    fun setAsInfoText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.INFO, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置中性文本样式
     */
    fun setAsNeutralText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.NEUTRAL, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置主色文本样式
     */
    fun setAsPrimaryText(
        level: TextLevel = TextLevel.PRIMARY,
        size: TextSize = TextSize.MEDIUM,
        weight: TextWeight = TextWeight.NORMAL
    ) {
        setTextStyle(TextType.PRIMARY, level, size, weight, isDarkMode)
    }
    
    /**
     * 设置深色模式
     */
    fun setDarkMode(darkMode: Boolean) {
        if (this.isDarkMode != darkMode) {
            this.isDarkMode = darkMode
            applySemanticStyle()
        }
    }
    
    /**
     * 获取当前文本类型
     */
    fun getTextType(): TextType = textType
    
    /**
     * 获取当前文本层级
     */
    fun getTextLevel(): TextLevel = textLevel
    
    /**
     * 获取当前文本大小类型
     */
    fun getTextSizeType(): TextSize = textSizeType
    
    /**
     * 获取当前字体粗细
     */
    fun getTextWeight(): TextWeight = textWeight
    
    /**
     * 是否为深色模式
     */
    fun isDarkMode(): Boolean = isDarkMode
    
    companion object {
        /**
         * 创建语义化文本的工厂方法
         */
        fun createSemanticText(
            context: Context,
            type: TextType,
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL,
            text: String = "",
            darkMode: Boolean = false
        ): SemanticTextView {
            return SemanticTextView(context).apply {
                setTextStyle(type, level, size, weight, darkMode)
                if (text.isNotEmpty()) {
                    this.text = text
                }
            }
        }
        
        /**
         * 创建成功文本
         */
        fun createSuccessText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.SUCCESS, level, size, weight, text)
        }
        
        /**
         * 创建警告文本
         */
        fun createWarningText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.WARNING, level, size, weight, text)
        }
        
        /**
         * 创建危险文本
         */
        fun createDangerText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.DANGER, level, size, weight, text)
        }
        
        /**
         * 创建信息文本
         */
        fun createInfoText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.INFO, level, size, weight, text)
        }
        
        /**
         * 创建中性文本
         */
        fun createNeutralText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.NEUTRAL, level, size, weight, text)
        }
        
        /**
         * 创建主色文本
         */
        fun createPrimaryText(
            context: Context,
            text: String = "",
            level: TextLevel = TextLevel.PRIMARY,
            size: TextSize = TextSize.MEDIUM,
            weight: TextWeight = TextWeight.NORMAL
        ): SemanticTextView {
            return createSemanticText(context, TextType.PRIMARY, level, size, weight, text)
        }
    }
}